package ahblue.cn.tanzi.ui.person;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import ahblue.cn.tanzi.R;
import ahblue.cn.tanzi.jmessage.im.util.ToastUtil;
import ahblue.cn.tanzi.model.user.CodeModel;
import ahblue.cn.tanzi.model.user.LoginResultModel;
import ahblue.cn.tanzi.model.user.UserModel;
import ahblue.cn.tanzi.ui.base.BaseActivity;
import ahblue.cn.tanzi.util.ImageLoadUtil;
import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import cn.jpush.im.android.api.JMessageClient;
import cn.jpush.im.android.api.model.UserInfo;
import cn.jpush.im.api.BasicCallback;

/**
 * Created by fengdongfei on 2018/12/1.
 */

public class PeopleMsgActivity extends BaseActivity {
    @BindView(R.id.icon)
    ImageView icon;
    @BindView(R.id.tvnickname)
    TextView tvnickname;
    @BindView(R.id.tvreal)
    TextView tvreal;
    @BindView(R.id.tvbind)
    TextView tvbind;
    @BindView(R.id.tvsign)
    TextView tvsign;
    private UserModel model;
    private LoginResultModel loginmodel;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_people_update);
        ButterKnife.bind(this);
        model = LoginResultModel.getUserAllModel(this, UserModel.class);
        loginmodel = LoginResultModel.getUserModel(this, LoginResultModel.class);

    }

    @Override
    protected void onResume() {
        super.onResume();
        UserModel.getUserInfo(this, LoginResultModel.getToken(this), new UserModel.UserInfoLoginStatus() {
            @Override
            public void conver(UserModel models) {
                model = LoginResultModel.getUserAllModel(PeopleMsgActivity.this, UserModel.class);
            }
        });
        ImageLoadUtil.load(this, model.member_avatar, icon);
        tvnickname.setText(model.member_name);
        tvbind.setText(model.member_mobile);
        tvsign.setText(model.profile);
        if (model.has_real == 0) {
            tvreal.setText("未认证");
        } else {
            tvreal.setText("已认证");
        }
    }

    @OnClick({R.id.llicon, R.id.llnick, R.id.llfile, R.id.llreal, R.id.btn_back,R.id.bindphone})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.llicon:
                break;
            case R.id.bindphone:
                startActivity(new Intent(this,UpdatePhoneActivity.class));
                break;
            case R.id.btn_back:
                finish();
                break;
            case R.id.llnick:
                showInputDialog("修改昵称", 0);
                break;
            case R.id.llfile:
                showInputDialog("编辑个人简介", 1);
                break;
            case R.id.llreal:
                if (model.has_real == 0) {
                    Intent in = new Intent(this, RealActivity.class);
                    startActivity(in);
                } else {

                }
                break;
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
    }


    //https://www.cnblogs.com/gzdaijie/p/5222191.html
    private void showInputDialog(String title, int id) {
    /*
        @setView 装入一个EditView
     */
        final EditText editText = new EditText(PeopleMsgActivity.this);
        AlertDialog.Builder inputDialog =
                new AlertDialog.Builder(PeopleMsgActivity.this);
        inputDialog.setTitle(title).setView(editText);
        inputDialog.setPositiveButton("确定",
                new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        if (id == 0) {
                            // 修改昵称
                            CodeModel.setNiceName(PeopleMsgActivity.this, editText.getText().toString(),
                                    LoginResultModel.getToken(PeopleMsgActivity.this), new CodeModel.VertifyListener() {
                                        @Override
                                        public void conver(boolean b) {
                                            UserInfo myInfo = JMessageClient.getMyInfo();
                                            if (myInfo != null) {
                                                myInfo.setNickname(editText.getText().toString());
                                            }
                                            JMessageClient.updateMyInfo(UserInfo.Field.nickname, myInfo, new BasicCallback() {
                                                @Override
                                                public void gotResult(int i, String s) {
                                                    if (i == 0) {
                                                        dialog.dismiss();
                                                        tvnickname.setText(editText.getText().toString());
                                                        Log.i("UpdateUserInfoActivity", "updateNickName," + " responseCode = " + i + "; desc = " + s);
                                                    } else {
                                                        dialog.dismiss();
                                                        ToastUtil.shortToast(PeopleMsgActivity.this,"修改失败");
                                                        Log.i("UpdateUserInfoActivity", "updateNickName," + " responseCode = " + i + "; desc = " + s);
                                                    }
                                                }
                                            });

                                        }
                                    });
                        } else {
                            CodeModel.setProfit(PeopleMsgActivity.this, editText.getText().toString(),
                                    LoginResultModel.getToken(PeopleMsgActivity.this), new CodeModel.DataVertifyListener() {
                                        @Override
                                        public void conver(String b) {
                                            dialog.dismiss();
                                            if (!TextUtils.isEmpty(b)){
                                                tvsign.setText(b);
                                            }
                                        }
                                    });
                        }
                    }
                }).show();
    }
}
